package com.lxp.eduService.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxp.eduService.domain.EduCourse;
import com.lxp.eduService.domain.EduTeacher;
import com.lxp.eduService.domain.vo.front.TeacherFrontVo;
import com.lxp.eduService.service.EduCourseService;
import com.lxp.eduService.service.EduTeacherService;
import com.lxp.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
//前端控制器 用来获取课程 讲师信息
@RequestMapping("/eduService/front")
public class FrontController {
    @Autowired
    private EduTeacherService teacherService;
    @Autowired
    private EduCourseService courseService;
    //获取八位讲师
    @GetMapping("/getTeacherList")
    public R getTeacherList(){
        //根据讲师级别以及sort来排序
        QueryWrapper<EduTeacher> wrapper =new QueryWrapper<>();
        wrapper.orderByDesc("level","sort");
        wrapper.last("limit 8");
        List<EduTeacher> list = teacherService.list(wrapper);

        return R.ok().data("teacherList",list);
    }


    //获取八门课程
    @GetMapping("/getCourseList")
    public R getCourseList(){
        //根据浏览次数来获取
        QueryWrapper<EduCourse> wrapper =new QueryWrapper<>();
        wrapper.orderByDesc("view_count");
        wrapper.last("limit 8");

        //根据
        List<EduCourse> list = courseService.list(wrapper);
        return R.ok().data("courseList",list);
    }


    //条件分页查询讲师列表
    @PostMapping("/getTeacherList/{current}/{limit}")
    public R getTeacherList(@PathVariable long current
            ,@PathVariable long limit
            ,@RequestBody(required = false) TeacherFrontVo teacherFrontVo){
        Page<EduTeacher> page =new Page<>(current,limit);
        Map<String,Object> map =teacherService.getTeacherListFront(page,teacherFrontVo);
        return R.ok().data(map);
    }

    //根据讲师ID查询讲师信息以及该讲师的课程列表信息
    @GetMapping("/getTeacherDataById/{id}")
    public R getTeacherDataById(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        QueryWrapper<EduCourse> wrapper =new QueryWrapper<>();
        wrapper.eq( "teacher_id",id);
        //所讲的课程列表
        List<EduCourse> courseList = courseService.list(wrapper);
        return R.ok().data("teacher",teacher).data("courseList",courseList);
    }
}
